package com.thinkfit.jy.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.common.lib.constant.Constants;
import com.common.lib.fragment.BaseFragment;
import com.common.lib.manager.DataManager;
import com.common.lib.mvp.contract.EmptyContract;
import com.common.lib.mvp.presenter.EmptyPresenter;
import com.jaygoo.widget.OnRangeChangedListener;
import com.jaygoo.widget.RangeSeekBar;
import com.thinkfit.jy.R;

import java.math.BigDecimal;

/**
 * 设置目标界面
 */
public class SetTargetFragment extends BaseFragment<EmptyContract.Presenter> implements EmptyContract.View {

    private int mIndex;

    private int initialValue;

    RangeSeekBar bar;
    @NonNull
    @Override
    protected EmptyContract.Presenter onCreatePresenter() {
        return new EmptyPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_set_target;
    }

    @Override
    protected void initView(@NonNull View view, @Nullable Bundle savedInstanceState) {
        bar = view.findViewById(R.id.rsb);
        if (DataManager.Companion.getInstance().getMainStyle() == 3) {
            view.setBackgroundResource(0);
            bar.setProgressDefaultDrawableId(R.drawable.shape_ccffffff_7);
        }
        setViewsOnClickListener(R.id.ivMinus, R.id.ivAdd);
        initValue();
//        mIndex = getArguments().getInt(Constants.BUNDLE_EXTRA);
//        if (mIndex == 0) {
//            bar.setRange(0f, 99f);
//            initialValue = 30;
//            setText(R.id.tvUnit, R.string.app_minutes);
//        } else if (mIndex == 1) {
//            bar.setRange(0f, 99f);
//            initialValue = 5;
//            setText(R.id.tvUnit, R.string.app_km);
//        } else {
//            bar.setRange(0f, 990f);
//            initialValue = 300;
//            setText(R.id.tvUnit, R.string.app_kcal);
//        }
//        bar.setProgress(initialValue);
//        setText(R.id.tvNum,initialValue+"");
        setViewsOnClickListener(R.id.tvOk);

        setTextTypeFace(R.id.tvNum);
        bar.setOnRangeChangedListener(new OnRangeChangedListener() {
            @Override
            public void onRangeChanged(RangeSeekBar view, float leftValue, float rightValue, boolean isFromUser) {
                setText(R.id.tvNum, String.valueOf((int) (leftValue + 0.0001)));
            }

            @Override
            public void onStartTrackingTouch(RangeSeekBar view, boolean isLeft) {

            }

            @Override
            public void onStopTrackingTouch(RangeSeekBar view, boolean isLeft) {

            }
        });
    }

    public void initValue(){
        mIndex = getArguments().getInt(Constants.BUNDLE_EXTRA);
        if (mIndex == 0) {
            bar.setRange(0f, 99f);
            initialValue = 30;
            setText(R.id.tvUnit, R.string.app_minutes);
        } else if (mIndex == 1) {
            bar.setRange(0f, 99f);
            initialValue = 5;
            setText(R.id.tvUnit, R.string.app_km);
        } else {
            bar.setRange(0f, 990f);
            initialValue = 300;
            setText(R.id.tvUnit, R.string.app_kcal);
        }
        bar.setProgress(initialValue);
        setText(R.id.tvNum,initialValue+"");
    }

    @Override
    public void onResume() {
        super.onResume();
        initValue();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvOk:
                int num = Integer.parseInt(getTextById(R.id.tvNum));
                if(num <= 0){
                    showToast(R.string.app_toast_hint_1);
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra(Constants.BUNDLE_EXTRA, mIndex);
                intent.putExtra(Constants.BUNDLE_EXTRA_2, num);
                getActivity().setResult(Activity.RESULT_OK, intent);
                finish();
                break;
            case R.id.ivMinus:
                resetNum(false);
                break;
            case R.id.ivAdd:
                resetNum(true);
                break;
        }
    }

    private void resetNum(boolean isAdd) {
        BigDecimal num = new BigDecimal(getTextById(R.id.tvNum));
        BigDecimal dx = BigDecimal.ONE;
        BigDecimal max = new BigDecimal("99");
        if (mIndex == 2) {
            dx = BigDecimal.TEN;
            max = new BigDecimal("990");
        }
        if (isAdd) {
            num = num.add(dx);
        } else {
            num = num.subtract(dx);
        }
        if (num.compareTo(BigDecimal.ZERO) < 0 || num.compareTo(max) > 0) {
            return;
        }
        RangeSeekBar bar = getView().findViewById(R.id.rsb);
        bar.setProgress(num.intValue());
        setText(R.id.tvNum, String.valueOf(num.intValue()));
    }

    public static SetTargetFragment getInstance(int index) {
        Bundle bundle = new Bundle();
        bundle.putInt(Constants.BUNDLE_EXTRA, index);
        SetTargetFragment fragment = new SetTargetFragment();
        fragment.setArguments(bundle);
        return fragment;
    }
}
